package com.zg.job.batch;

import com.zg.job.config.CustomJobExecutionListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;

import javax.annotation.Resource;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/3/3 10:04
 */

@Component
@Slf4j
public class ThirdJobDo {

    @Resource
    private JobBuilderFactory jobBuilderFactory;
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Autowired
    private CustomJobExecutionListener customJobExecutionListener;

    @Autowired
    private PaymentProcessingTasklet paymentProcessingTasklet;

    @Bean
    public Job paymentJob(Step paymentStep) {
        return jobBuilderFactory.get("paymentJob")
                .incrementer(new RunIdIncrementer())
                .start(paymentStep)
                .build();
    }

    @Bean
    public Step paymentStep(RetryTemplate retryTemplate) {
        return stepBuilderFactory.get("paymentStep")
                .tasklet(paymentProcessingTasklet)
                .transactionManager(transactionManager)
                .build();
    }

    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();

        // 重试策略：最多重试3次
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(3);
        retryTemplate.setRetryPolicy(retryPolicy);

        // 退避策略：每次重试间隔2秒
        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
        backOffPolicy.setBackOffPeriod(2000);
        retryTemplate.setBackOffPolicy(backOffPolicy);

        return retryTemplate;
    }

//    // 定义重试策略（最多重试3次）
//    @Bean
//    public SimpleRetryPolicy retryPolicy() {
//        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
//        retryPolicy.setMaxAttempts(3);
//        return retryPolicy;
//    }
//
//    // 定义退避策略（每次间隔2秒）
//    @Bean
//    public FixedBackOffPolicy backOffPolicy() {
//        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
//        backOffPolicy.setBackOffPeriod(2000); // 2秒
//        return backOffPolicy;
//    }


}
